package com.canaan.web.controller.monitor;

import com.canaan.common.core.controller.BaseController;
import com.canaan.common.core.domain.R;
import com.canaan.common.core.page.TableDataInfo;
import com.canaan.system.domain.vo.PageQuery;
import com.canaan.system.domain.vo.SysLogFileBo;
import com.canaan.system.domain.vo.SysLogFileVo;
import com.canaan.system.service.ISysLogFileService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Comparator;
import java.util.Map;
import java.util.stream.Collectors;

import static com.canaan.common.utils.PageUtils.startPage;

/**
 * 日志文件 操作处理
 *
 * @author
 */

@Controller
@RequestMapping("/monitor/logFile")
public class SysLogFileController extends BaseController {
    private String prefix = "monitor/logFile";

    @Autowired
    private ISysLogFileService logFileService;

    //@RequiresPermissions("monitor:logFile:view")
    @GetMapping()
    public String logFile() {
        return prefix + "/logFile";
    }

    /**
     * 获取日志文件列表
     */
    @ApiOperation("获取日志文件列表")
    //@RequiresPermissions("monitor:logFile:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysLogFileBo param, PageQuery pageQuery) {
        startPage();
        TableDataInfo list = logFileService.list(param, pageQuery);
        return list;
    }

    /**
     * 查询日志文件尾行内容
     */
    //@RequiresPermissions("monitor:logFile:view")
    @GetMapping("/tail")
    public R<Map<String, String>> tail(SysLogFileBo param) {
        return R.ok(logFileService.tail(param));
    }

    /**
     * 下载日志文件
     */
    //@RequiresPermissions("monitor:logFile:download")
    @ApiOperation("下载文件")
    @GetMapping("/download")
    public void download(SysLogFileBo param, HttpServletResponse response) throws IOException {
        logFileService.download(param, response);
    }

    /**
     * 删除日志文件
     **/
    @PreAuthorize("@ss.hasPermi('monitor:logFile:delete')")
    @DeleteMapping("/delete")
    public R<Void> delete(SysLogFileBo param) {
        if (logFileService.delete(param.getPath())) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

}
